<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="还款流水号" prop="repaymentNo">
        <el-input
          v-model="queryParams.repaymentNo"
          placeholder="请输入还款流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户号" prop="custNo">
        <el-input
          v-model="queryParams.custNo"
          placeholder="请输入客户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bizsys:order:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="PK" align="center" prop="id"/>
      <el-table-column label="还款流水号" align="center" prop="repaymentNo"/>
      <el-table-column label="客户号" align="center" prop="custNo"/>
      <el-table-column label="还款渠道" align="center" prop="actionType"/>
      <el-table-column label="应还本金下限" align="center" prop="repaymentAmtMin"/>
      <el-table-column label="减免次数上限" align="center" prop="numLimit"/>
      <el-table-column label="比例" align="center" prop="rate"/>
      <el-table-column label="截止日期" align="center" prop="dayLastAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dayLastAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="减免最小金额" align="center" prop="reduceAmtMin"/>
      <el-table-column label="减免上限金" align="center" prop="reduceAmtMax"/>
      <el-table-column label="iou服务费" align="center" prop="serviceFee"/>
      <el-table-column label="实际减免金额" align="center" prop="currentReduceAmt"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户减免订单记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="还款流水号" prop="repaymentNo">
          <el-input v-model="form.repaymentNo" placeholder="请输入还款流水号"/>
        </el-form-item>
        <el-form-item label="客户号" prop="custNo">
          <el-input v-model="form.custNo" placeholder="请输入客户号"/>
        </el-form-item>
        <el-form-item label="还款渠道" prop="actionType">
          <el-select v-model="form.actionType" placeholder="请选择还款渠道">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="应还本金下限" prop="repaymentAmtMin">
          <el-input v-model="form.repaymentAmtMin" placeholder="请输入应还本金下限"/>
        </el-form-item>
        <el-form-item label="减免次数上限" prop="numLimit">
          <el-input v-model="form.numLimit" placeholder="请输入减免次数上限"/>
        </el-form-item>
        <el-form-item label="比例" prop="rate">
          <el-input v-model="form.rate" placeholder="请输入比例"/>
        </el-form-item>
        <el-form-item label="截止日期" prop="dayLastAt">
          <el-date-picker clearable size="small"
                          v-model="form.dayLastAt"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择截止日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="减免最小金额" prop="reduceAmtMin">
          <el-input v-model="form.reduceAmtMin" placeholder="请输入减免最小金额"/>
        </el-form-item>
        <el-form-item label="减免上限金" prop="reduceAmtMax">
          <el-input v-model="form.reduceAmtMax" placeholder="请输入减免上限金"/>
        </el-form-item>
        <el-form-item label="iou服务费" prop="serviceFee">
          <el-input v-model="form.serviceFee" placeholder="请输入iou服务费"/>
        </el-form-item>
        <el-form-item label="实际减免金额" prop="currentReduceAmt">
          <el-input v-model="form.currentReduceAmt" placeholder="请输入实际减免金额"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder} from "@/api/bizsys/custReduceOrder";

export default {
  name: "custReduceOrder",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户减免订单记录表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repaymentNo: null,
        custNo: null,
        actionType: null,
        repaymentAmtMin: null,
        numLimit: null,
        rate: null,
        dayLastAt: null,
        reduceAmtMin: null,
        reduceAmtMax: null,
        serviceFee: null,
        currentReduceAmt: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        custNo: [
          {required: true, message: "客户号不能为空", trigger: "blur"}
        ],
        actionType: [
          {required: true, message: "还款渠道不能为空", trigger: "change"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    // this.getList();
    this.getDicts("repay_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询客户减免订单记录列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        repaymentNo: null,
        custNo: null,
        actionType: null,
        repaymentAmtMin: null,
        numLimit: null,
        rate: null,
        dayLastAt: null,
        reduceAmtMin: null,
        reduceAmtMax: null,
        serviceFee: null,
        currentReduceAmt: null,
        status: 0,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户减免订单记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户减免订单记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除客户减免订单记录编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客户减免订单记录数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportOrder(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
